/*package com.ssmk.module.shoppingcar.order;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.liaoinstan.springview.widget.SpringView;
import com.ssmk.R;
import com.ssmk.base.BaseView;
import com.ssmk.base.MVPBaseFragment;
import com.ssmk.module.home.view.Home_LoadMoreFooter;
import com.ssmk.module.home.view.Home_RefreshHead;
import com.ssmk.module.shoppingcar.adapter.AllOrderAdapter;
import com.ssmk.module.shoppingcar.adapter.SaleAfterAdapter;
import com.ssmk.module.shoppingcar.bean.CancelOrderBean;
import com.ssmk.module.shoppingcar.bean.OrderAllBean;
import com.ssmk.module.shoppingcar.bean.SaleAfterBean;
import com.ssmk.module.shoppingcar.presenter.AllOrderPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AllOrder_Fragment_all<T> extends MVPBaseFragment<T> {

    @BindView(R.id.allorder_springView)
    SpringView mAllorderSpringView;
    private List<OrderAllBean.DataBean> list = new ArrayList<>();
    @BindView(R.id.allorder_recycler)
    RecyclerView mAllorderRecycler;
    Unbinder unbinder;
    private int page = 0;
    private AllOrderPresenter mAllOrderPresenter;

    private String mString;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    protected void initdata() {
        mString = "全部";
        mAllOrderPresenter = new AllOrderPresenter(getActivity(), this, mString, page);
        mAllOrderPresenter.GetDataModel();

        mAllorderSpringView.setType(SpringView.Type.FOLLOW);
        mAllorderSpringView.setHeader(new Home_RefreshHead());
        mAllorderSpringView.setFooter(new Home_LoadMoreFooter());
        mAllorderSpringView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                mAllorderSpringView.onFinishFreshAndLoad();
            }
            @Override
            public void onLoadmore() {
                page++;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mAllOrderPresenter.GetDataModel();
                        mAllorderSpringView.onFinishFreshAndLoad();
                    }
                }, 2000);
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected int layoutRes() {
        return R.layout.allorder_fragment;
    }

    @Override
    public void UpData(Object o) {
        if (o != null) {
            if (o instanceof OrderAllBean) {
                OrderAllBean orderAllBean = (OrderAllBean) o;
                List<OrderAllBean.DataBean> data = orderAllBean.getData();
                LogUtils.e("DataBean==",data.toString());
                if (data != null && data.size() > 0) {
                    for (int i = 0; i < data.size(); i++) {
                        list.add(data.get(i));
                    }
                    LinearLayoutManager manager = new LinearLayoutManager(getActivity());
                    mAllorderRecycler.setLayoutManager(manager);

                    AllOrderAdapter adapter = new AllOrderAdapter(getActivity(), list);
                    mAllorderRecycler.setAdapter(adapter);//订单适配器

                   *//* adapter.setOnItemClickListener(new AllOrderAdapter.OnItemClickListener() {
                        @Override
                        public void onItemClick(View view, int position) {
                            Intent intent = new Intent(getActivity(), WaitPayActivity.class);
                            startActivity(intent);
                        }
                    });*//*
                }
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
*/

package com.ssmk.module.shoppingcar.order;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.liaoinstan.springview.widget.SpringView;
import com.ssmk.R;
import com.ssmk.base.MVPBaseFragment;
import com.ssmk.module.home.view.Home_LoadMoreFooter;
import com.ssmk.module.home.view.Home_RefreshHead;
import com.ssmk.module.shoppingcar.adapter.AllOrderAdapter;
import com.ssmk.module.shoppingcar.bean.OrderAllBean;
import com.ssmk.module.shoppingcar.contract.AllOrderContract;
import com.ssmk.module.shoppingcar.presenter.AllOrderPresenter;
import com.ssmk.module.shoppingcar.window.CancelOrderWindow;
import com.ssmk.utlis.BinderHelper;
import com.ssmk.utlis.Logger;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AllOrder_Fragment_all<T> extends MVPBaseFragment<T> implements AllOrderContract.View<T>, AllOrderContract.OnOrderButtonListener {

    @BindView(R.id.allorder_springView)
    SpringView mAllorderSpringView;

    @BindView(R.id.allorder_recycler)
    RecyclerView mAllorderRecycler;

    Unbinder unbinder;

    private int page = 0;

    private List<OrderAllBean.DataBean> list = new ArrayList<>();

    private AllOrderPresenter mAllOrderPresenter;

    private AllOrderAdapter adapter;

    private String mOrderType;

    public static AllOrder_Fragment_all getInstance(String type) {
        AllOrder_Fragment_all fragment = new AllOrder_Fragment_all();
        Bundle args = new Bundle();
        args.putString("order_type", type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }


    @Override
    protected void initdata() {
        Bundle args = getArguments();
        if (null == args) {
            return;
        }
        mOrderType = args.getString("order_type", "");
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        mAllorderRecycler.setLayoutManager(manager);
        adapter = new AllOrderAdapter(getActivity(), list);
        adapter.setOnOrderButtonListener(this);
        mAllorderRecycler.setAdapter(adapter);//订单适配器
        adapter.setOnItemClickListener(new AllOrderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //TODO item点击事件
//                Intent intent = new Intent(getActivity(), WaitPayActivity.class);
//                startActivity(intent);
            }
        });
        mAllOrderPresenter = new AllOrderPresenter(this);
        getOrderData();
        mAllorderSpringView.setType(SpringView.Type.FOLLOW);
        mAllorderSpringView.setHeader(new Home_RefreshHead());
        mAllorderSpringView.setFooter(new Home_LoadMoreFooter());
        mAllorderSpringView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page = 0;
                Logger.i("********  onRefresh   " + page);
                getOrderData();
            }

            @Override
            public void onLoadmore() {
                page++;
                Logger.i("********  onLoadmore   " + page);
                getOrderData();
            }
        });
    }

    /**
     * 判断是哪个页面
     */
    private void getOrderData() {
        if (null == mAllOrderPresenter)
            return;
        if ("全部".equals(mOrderType)) {
            mAllOrderPresenter.queryAllOrder(page);
        } else if ("待付款".equals(mOrderType)) {
            mAllOrderPresenter.waitPayOrder(page);
        } else if ("待发货".equals(mOrderType)) {
            mAllOrderPresenter.waitSendOrder(page);
        } else if ("待收货".equals(mOrderType)) {
            mAllOrderPresenter.waitGetOrder(page);
        } else {
            mAllOrderPresenter.saleAfterOrder(page);
        }
    }

    @Override
    protected void initView() {

    }

    @Override
    protected int layoutRes() {
        return R.layout.allorder_fragment;
    }

    @Override
    public void UpData(Object o) {
        if (mAllorderSpringView != null)
            mAllorderSpringView.onFinishFreshAndLoad();
        if (adapter == null)
            return;
        if (o != null) {
            if (o instanceof OrderAllBean) {
                OrderAllBean orderAllBean = (OrderAllBean) o;
                List<OrderAllBean.DataBean> data = orderAllBean.getData();
                if (data != null && data.size() > 0) {
                    if (page == 0)
                        list.clear();
                    list.addAll(data);
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (null != mAllOrderPresenter) {
            mAllOrderPresenter.stop();
            mAllOrderPresenter = null;
        }
        BinderHelper.unbinder(unbinder);
    }

    @Override
    public void showToast(String text) {
        if (null != getContext()) {
            Toast.makeText(getContext(), text, Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 取消订单接口返回
     */
    @Override
    public void cancelOrderResult(String orderId, String cancelFlag) {
        if (null == adapter)
            return;
        //确保取消的是用户点击的订单
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getOrderId().equals(orderId)) {
                list.get(i).setFlag(cancelFlag);
                Logger.i("取消订单id ", "" + orderId);
                break;
            }
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 删除订单接口返回
     */
    @Override
    public void deleteOrderResult(String orderId) {
        if (null == adapter)
            return;
        //确保取消的是用户点击的订单
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getOrderId().equals(orderId)) {
                list.remove(i);
                Logger.i("删除订单id ", "" + orderId);
                break;
            }
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 取消订单adapter listener回调
     */
    @Override
    public void cancelOrder(final String orderId) {
        //弹出取消订单popwindow
        setWindowAlpha(0.4f);
        CancelOrderWindow window = CancelOrderWindow.initPopupWindow(getContext());
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setWindowAlpha(1f);
            }
        });
        window.setOnCancelListener(new CancelOrderWindow.OnCancelListener() {
            @Override
            public void onCancelClick(String flag) {
                if (null == mAllOrderPresenter)
                    return;
                mAllOrderPresenter.cancelOrder(orderId, flag);
            }
        });
    }

    /**
     * 删除订单adapter listener回调
     */
    @Override
    public void deleteOrder(String orderId) {
        if (null == mAllOrderPresenter)
            return;
        mAllOrderPresenter.deleteOrder(orderId);
    }

    private void setWindowAlpha(float alpha) {
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = alpha;
        getActivity().getWindow().setAttributes(lp);
    }
}
